import axios from 'axios'
import router from '@/router'
import { Loading } from 'element-ui'
axios.defaults.baseURL = 'http://www.liulongbin.top:3008'
let fullScreenLoading
// 请求拦截器
axios.interceptors.request.use(function (config) {
  // 展示 loading 效果
  if (config.url.startsWith('/my')) {
    config.headers.Authorization = sessionStorage.getItem('token')
  }
  fullScreenLoading = Loading.service({
    text: '拼命加载中',
    fullscreen: true,
    background: 'rgba(0, 0, 0, 0.8)'
  })
  return config
}, function (error) {
  return Promise.reject(error)
})

// 全局挂载 - 响应拦截器
axios.interceptors.response.use(function (response) {
  // 隐藏 loading 效果
  fullScreenLoading.close()
  return response
}, function (error) {
  // 隐藏 loading 效果
  if (error.response.status === 401) {
    sessionStorage.removeItem('token')
    router.push('/login')
  }
  // 隐藏 loading 效果
  fullScreenLoading.close()
  return Promise.reject(error)
})
export default axios
